<template>
<el-container>
  <!-- 头部区域 -->
  <el-header>
    <div>
      <img src="..\assets\c2c.png" alt="" class="img">
      <span class="header-title">商品管理系统</span>
    </div>
    <el-button type="danger" @click="logout">退出</el-button>
  </el-header>
  <!-- 头部区域 -->
  <el-container>
    <!-- 左侧区域 -->
    <el-aside :width="isCollapse ? '64px' : '200px'">
      <div class="toggle-button" @click="toggleCollapse">|||</div>
      <!-- 侧边栏菜单区域  -->
      <!-- default-active 选中列表高亮 ;unique-open只展开一个;default==默认，不履行 -->
        <el-menu  :unique-opened="true" :collapse="isCollapse" :collapse-transition="false"
        router:default-active="activePath">
          <!-- 一级菜单 -->
      <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
        <!-- 一级菜单的模板区 -->
        <template slot="title">
          <i :class="iconsObj[item.id]"></i>
          <span>{{item.authName}}</span>
        </template>
        <!-- 二级菜单 -->
        <!-- saveNavStates保存  subItem.path 状态 保存在Session Storage -->
          <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>{{subItem.authName}}</span>
            </template>
          </el-menu-item>
      </el-submenu>
        </el-menu>
    </el-aside>
    <!-- 以下是主体内容 -->
    <el-main>
      <router-view>hello_world</router-view>
      </el-main>
  </el-container>
</el-container>
</template>

<script>
export default {
  data () {
    return {
      menulist: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 是否折叠 绑定在el-menu 上
      isCollapse: false,
      // 被激活的链接地址,动态绑定到el-menu上
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    // 动态页面
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 登出功能实现，sessionStorage清除，回到登录页面
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      console.log(res)
    },
    // 点击按钮，切换菜单的折叠与展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 获取保存链接的激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.$router.push(activePath)
      this.activePath = activePath
      console.log(activePath)
    }
  }
}
</script>

<style lang="less" scoped>
  .home-container {
    height: 100%;
    background-image: linear-gradient(#abedd8,#46cdcf,#3d84a8,#48466d);
  }
  .el-header {
    background-color: #53525336;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 3px solid #57e5f194;
  }
  .el-aside {
    background-color: #53525325;
    border: 5px rgb(95, 158, 160);
  }
  .el-main{
    background-color: #f0f1f600;
  }
  .el-menu {
    background-color:#46cdcf6c;
    color: #48466d;
  }
  .img{
    height: 40px;
    width:40px;
  }
  .el-container {
    height: 100%;
    background-image: linear-gradient(#abedd8,#46cdcf,#3d84a8,#48466d);
  }
  .toggle-button {
    background-color: #5352532a;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    /* 字符间距 */
    letter-spacing: 0.2em;
    cursor: pointer;
  }
  .header-title {
    color: rgb(245 108 108);
    padding-left: 20px;
    padding-top: 0;
    font-size: 28px;
  }
</style>
